package com.example.weatherforecast;


import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import java.util.List;

/**
 * ViewPaper加载Fragment的适配器
 */
public class CityFragmentPaperAdapter extends FragmentStatePagerAdapter {
    // 存放cityFragment
    List<Fragment> fragmentList;
    // 存放的页数
    int count = 0;

    public CityFragmentPaperAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        this.fragmentList = fragmentList;
    }

    /**
     * 根据position获取fragmenr
     *
     * @param position
     * @return
     */
    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    /**
     * 返回cityFragment的数量
     *
     * @return
     */
    @Override
    public int getCount() {
        return fragmentList.size();
    }

    /**
     * ViewPager页数发生变化
     */
    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        this.count = getCount();
    }

    /**
     * 获取位置
     * @param object
     * @return
     */
    @Override
    public int getItemPosition(@NonNull Object object) {
        if (count > 0) {
            count--;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }
}